home *** CD-ROM | disk | FTP | other *** search
/ Usenet 1993 July / InfoMagic USENET CD-ROM July 1993.ISO / sources / misc / volume2 / mpg < prev    next >
Encoding:
Internet Message Format  |  1991-08-07  |  18.5 KB

  1. From: wnp@dcs.UUCP
  2. Newsgroups: comp.sources.misc
  3. Subject: v02i017: MPG - Fuel Economy Calculator
  4. Message-ID: <7128@ncoast.UUCP>
  5. Date: 26 Jan 88 03:53:00 GMT
  6. Approved: allbery@ncoast.UUCP
  7.  
  8. Comp.sources.misc: Volume 2, Issue 17
  9. Submitted-By: Wolf Paul <wnp@dcs.UUCP>
  10. Archive-Name: mpg
  11.  
  12. I hope this is useful to some of the folks out there, if only
  13. as a sample of what can be done with the UNIX shell.
  14.  
  15. Wolf Paul
  16. ihnp4!killer!wnp
  17.  
  18. -------------------
  19. #! /bin/sh
  20. # This is a shell archive, meaning:
  21. # 1. Remove everything above the #! /bin/sh line.
  22. # 2. Save the resulting text in a file.
  23. # 3. Execute the file with /bin/sh (not csh) to create the files:
  24. #    README
  25. #    eurmpg
  26. #    fpdivide.1
  27. #    fpdivide.c
  28. #    mpg.1
  29. #    mpgdata
  30. #    ukmpg
  31. #    usmpg
  32. # This archive created: Sun Jan 17 18:10:21 1988 by ihnp4!killer!wnp
  33. export PATH; PATH=/bin:$PATH
  34. if test -f 'README'
  35. then
  36.     echo shar: will not over-write existing file "'README'"
  37. else
  38. cat << \SHAR_EOF > 'README'
  39. The enclosed files are an example of a floating point application
  40. written in Bourne shell scipt, with the aid of a short program,
  41. FPDIVIDE.
  42.  
  43. FPDIVIDE will divide, multiply, add, and subtract floating point
  44. numbers, depending on its name.
  45.  
  46. The sample application enclosed is a Fuel Economy Calculator,
  47. in versions for the US, UK, and the rest of the (decimal) world.
  48.  
  49. While fuel economy is not such a hot topic in the US, I don't know
  50. of any other topic more frequently discussed when drivers of automobiles
  51. talk about their cars in Europe.
  52.  
  53. This program will let you enter the distance driven and fuel consumed
  54. every time you fill your tank, and will then tell you how many miles
  55. to the gallon you have driven, or how many liters of fuel your have
  56. consumed per 100 km driven.
  57.  
  58. It will optionally print out a short statement on the standard line printer.
  59.  
  60. With minimal modifications, this program should also run under the
  61. MKS Toolkit implementation of the Korn Shell.
  62.  
  63. FILES:
  64.  
  65. README            this file
  66. fpdivide.c        the source for the floating point commands
  67. fpdivide.1        man page for the floating point commands
  68. mpg.1            man page for the MPG programs
  69. eurmpg            European/Decimal version of MPG
  70. ukmpg            UK/Imperial Gallons version of MPG
  71. usmpg            US version of MPG
  72. mpgdata            initial $HOME/.mpgdata file, all zeros
  73.  
  74. I hope sombody finds this useful. I welcome your comments!
  75.  
  76. Wolf N. Paul
  77. ihnp4!killer!wnp
  78. ihnp4!killer!dcs!wnp
  79. ihnp4!killer!doulos!wnp
  80. SHAR_EOF
  81. fi # end of overwriting check
  82. if test -f 'eurmpg'
  83. then
  84.     echo shar: will not over-write existing file "'eurmpg'"
  85. else
  86. cat << \SHAR_EOF > 'eurmpg'
  87. :
  88. # eurmpg  (Miles Per Gallon - European Version) -- A Fuel Economy Calculator
  89. # This shell program uses the floating point arithmetic commands
  90. # "fp*" to calculate fuel economy in terms of liters per 100 km,
  91. # miles per US gallon, and miles per UK (Imperial) gallon.
  92. #
  93. # As distributed, the program assumes that the user will enter 
  94. # the distance travelled in KILOMETERS, and the amount of fuel used
  95. # in LITERS, but that is not difficult to change to any other
  96. # combination of volume and distance measuring units.
  97. #
  98. # Author: Wolf N. Paul (ihnp4!killer!dcs!wnp), 1/13/88
  99. # Released into the Public Domain, Jan 18, 1988
  100. #
  101. # set -v # enable debugging
  102. #
  103. # CONSTANTS:
  104. #
  105. FILE=$HOME/.mpgdata
  106. US_GALLON=3.8
  107. UK_GALLON=4.5
  108. MILE=1.6
  109. #
  110. # Variables - all initialized to 0.0
  111. C_MILES=0.0        # current miles
  112. C_KMS=0.0        # current kilometers
  113. C_USGAL=0.0        # current gallons US
  114. C_UKGAL=0.0        # current gallons UK
  115. C_LITERS=0.0    # current liters
  116. T_MILES=0.0        # total miles
  117. T_KSM=0.0        # total kilometers
  118. T_USGAL=0.0        # total gallons US
  119. T_UKGAL=0.0        # total gallons UK
  120. T_LITERS=0.0    # total liters
  121. C_USMPG=0.0        # current miles/gallon US
  122. C_UKMPG=0.0        # current miles/gallon UK
  123. C_LP100=0.0        # current liters/100 km
  124. A_USMPG=0.0    # average miles/gallon US
  125. A_UKMPG=0.0    # average miles/gallon UK
  126. A_LP100=0.0    # average liters/100 km
  127. TMP=0.0            # kms/100
  128.  
  129. echo "MPG - Fuel Economy Calculator\n"
  130. echo "European (Decimal) Version - enter Kilometers and Liters\n"
  131.  
  132. echo "Reading old values ... \c"
  133.  
  134. T_MILES=`cut -d: -f1 $FILE`
  135. T_KMS=`cut -d: -f2 $FILE`
  136. T_USGAL=`cut -d: -f3 $FILE`
  137. T_UKGAL=`cut -d: -f4 $FILE`
  138. T_LITERS=`cut -d: -f5 $FILE`
  139. echo "done!\n"
  140.  
  141. echo "Enter kilometers driven: \c"
  142. read C_KMS
  143.  
  144. echo "Enter liters used: \c"
  145. read C_LITERS
  146.  
  147. echo "\nOne moment - calculating consumption figures ... \c"
  148. C_MILES=`fpdivide $C_KMS $MILE`
  149. C_USGAL=`fpdivide $C_LITERS $US_GALLON`
  150. C_UKGAL=`fpdivide $C_LITERS $UK_GALLON`
  151.  
  152. T_MILES=`fpadd $T_MILES $C_MILES`
  153. T_KMS=`fpadd $T_KMS $C_KMS`
  154. T_USGAL=`fpadd $T_USGAL $C_USGAL`
  155. T_UKGAL=`fpadd $T_UKGAL $C_UKGAL`
  156. T_LITERS=`fpadd $T_LITERS $C_LITERS`
  157.  
  158. C_USMPG=`fpdivide $C_MILES $C_USGAL`
  159. A_USMPG=`fpdivide $T_MILES $T_USGAL`
  160.  
  161. C_UKMPG=`fpdivide $C_MILES $C_UKGAL`
  162. A_UKMPG=`fpdivide $T_MILES $T_UKGAL`
  163.  
  164. TMP=`fpdivide $C_KMS 100.00`
  165. C_LP100=`fpdivide $C_LITERS $TMP`
  166. TMP=`fpdivide $T_KMS 100.00`
  167. A_LP100=`fpdivide $T_LITERS $TMP`
  168.  
  169. echo "$T_MILES:$T_KMS:$T_USGAL:$T_UKGAL:$T_LITERS" > $FILE
  170.  
  171. echo "done!"
  172. echo "\nFuel Economy:\n"
  173. echo "\t$C_LP100 liters per 100 km\t\t(Average ${A_LP100})"
  174. echo "\t$C_UKMPG miles per UK gallon\t(Average ${A_UKMPG})"
  175. echo "\t$C_USMPG miles per US gallon\t(Average ${A_USMPG})"
  176. echo "\nDo you want a printout of these results? \c"
  177. read REPLY
  178. if [ "$REPLY" != "y" -a "$REPLY" != "Y" ] ; then
  179.     exit
  180. fi
  181. echo "
  182. MPG (Eur) -- Fuel Economy Calculator
  183. Fuel Economy Statement as of `date '+%h %d, 19%y'`
  184.  
  185. Kilometers this Filling:   $C_KMS\t($C_MILES miles)
  186. Liters this Filling:       $C_LITERS\t($C_USGAL US, $C_UKGAL UK)
  187.  
  188. Total Kilometers so far:   $T_KMS\t($T_MILES miles)
  189. Total Liters so far:       $T_LITERS\t($T_USGAL US, $T_UKGAL UK)
  190.  
  191. Current Liters per 100 km: $C_LP100\t($C_USMPG US, $C_UKMPG UK)
  192. Average Liters per 100 km: $A_LP100\t($A_USMPG US, $A_UKMPG UK)\
  193. " | lp -s &
  194. SHAR_EOF
  195. fi # end of overwriting check
  196. if test -f 'fpdivide.1'
  197. then
  198.     echo shar: will not over-write existing file "'fpdivide.1'"
  199. else
  200. cat << \SHAR_EOF > 'fpdivide.1'
  201. .TH FPDIVIDE 1L Local
  202. .SH NAME
  203. fpdivide, fpmultiply, fpadd, fpsubtract \-
  204. floating point math programs for shell scripts
  205. .SH SYNOPSIS
  206. fpdivide
  207. .I num1 num2
  208. # performs the division
  209. .I num1 / num2
  210.  
  211. fpmultiply
  212. .I num1 num2
  213. # performs the multiplication
  214. .I num1 * num2
  215.  
  216. fpadd
  217. .I num1 num2
  218. # performs the addition
  219. .I num1 + num2
  220.  
  221. fpsubtract
  222. .I num1 num2
  223. # performs the subtraction
  224. .I num1 - num2
  225.  
  226. .SH DESCRIPTION
  227. These commands, which are really links to the same program, perform
  228. the floating point arithmetical operations as listed above, and print
  229. their result (with a precision of two decimal digits) on the standard
  230. output.
  231.  
  232. They are designed to add simple floating point arithmetic to Bourne and
  233. Korn shell scripts.
  234.  
  235. .SH INSTALLATION
  236. Compile the source file,
  237. .I fpdivide.c,
  238. copy the resulting executable into your local
  239. .I bin
  240. directory, and make additional links
  241. .I fpmultiply, fpadd,
  242. and
  243. .I fpsubtract
  244. to the same binary.
  245.  
  246. .SH AUTHOR
  247. Wolf N. Paul, ihnp4!killer!dcs!wnp
  248. .br
  249. Released into the Public Domain, Jan 18, 1988
  250.  
  251. SHAR_EOF
  252. fi # end of overwriting check
  253. if test -f 'fpdivide.c'
  254. then
  255.     echo shar: will not over-write existing file "'fpdivide.c'"
  256. else
  257. cat << \SHAR_EOF > 'fpdivide.c'
  258. /* fpdivide.c - floating point math program
  259.  *
  260.  * The program's action depends on its name:
  261.  *
  262.  * fpdivide        divide two floating point numbers
  263.  * fpmultiply    multiply two floating point numbers
  264.  * fpadd        add two floating point numbers
  265.  * fpsubtract    subtract two floating point numbers
  266.  *
  267.  * In any case, the program takes two arguments and prints its result
  268.  * to the standard output.
  269.  *
  270.  * The program was written to add floating point arithmetic to shell programs.
  271.  *
  272.  * Author: Wolf N. Paul, ihnp4!killer!dcs
  273.  * 
  274.  * Released into the Public Domain, Jan 18, 1988
  275.  */
  276.  
  277. #include <stdio.h>
  278.  
  279.  
  280. char *basename(s)
  281. char *s;
  282. {
  283.     char *base;
  284.     char *strrchr();
  285.  
  286.     if ( ( base = strrchr(s, '/')) != NULL)
  287.         return(&base[1]);
  288.     return(s);
  289. }
  290.  
  291.  
  292. main(argc, argv)
  293. int argc;
  294. char **argv;
  295. {
  296.     int division, multiplication, addition, subtraction;
  297.     char *progname;
  298.     char *basename();
  299.     float dividend, divisor, quotient;
  300.     float factor1, factor2, product;
  301.     float num1, num2, sum;
  302.     float atof();
  303.  
  304.     progname = basename(argv[0]);
  305.  
  306.     if ( strcmp(progname, "fpdivide") == 0 )
  307.         division = 1;
  308.     else if ( strcmp(progname, "fpmultiply") == 0 )
  309.         multiplication = 1;
  310.     else if ( strcmp(progname, "fpadd") == 0 )
  311.         addition = 1;
  312.     else if ( strcmp(progname, "fpsubtract") == 0)
  313.         subtraction = 1;
  314.  
  315.     if ( argc != 3 )
  316.     {
  317.         if ( division )
  318.             fprintf(stderr,"Usage: %s dividend divisor\n\n", progname);
  319.         else if ( multiplication )
  320.             fprintf(stderr,"Usage: %s factor1 factor2\n\n", progname);
  321.         else if ( addition || subtraction)
  322.             fprintf(stderr,"Usage: %s num1 num1\n\n", progname);
  323.         exit(-1);
  324.     }
  325.  
  326.     if ( division )
  327.     {
  328.         dividend = atof(argv[1]);
  329.         divisor  = atof(argv[2]);
  330.         if ( divisor <= 0.0 )
  331.         {
  332.             fprintf(stderr,"%s: divisor is zero or less.\n",
  333.                     argv[0]);
  334.             exit(-2);
  335.         }
  336.         quotient = dividend / divisor;
  337.         fprintf(stdout,"%.2f\n", quotient);
  338.     }
  339.     else if ( multiplication )
  340.     {
  341.         factor1 = atof(argv[1]);
  342.         factor2 = atof(argv[2]);
  343.         product = factor1 * factor2;
  344.         fprintf(stdout,"%.2f\n", product);
  345.     }
  346.     else if ( addition || subtraction )
  347.     {
  348.         num1 = atof(argv[1]);
  349.         num2 = atof(argv[2]);
  350.         if ( addition ) sum = num1 + num2;
  351.         else if ( subtraction ) sum = num1 - num2;
  352.         fprintf(stdout,"%.2f\n", sum);
  353.     }
  354. }
  355. SHAR_EOF
  356. fi # end of overwriting check
  357. if test -f 'mpg.1'
  358. then
  359.     echo shar: will not over-write existing file "'mpg.1'"
  360. else
  361. cat << \SHAR_EOF > 'mpg.1'
  362. .TH MPG 1L Local
  363. .SH NAME
  364. mpg, usmpg, ukmpg, eurmpg \- Miles Per Gallon, A Fuel Economy Calculator
  365. .SH SYNOPSIS
  366. mpg
  367. .SH DESCRIPTION
  368. .B Mpg
  369. is a
  370. .I fuel economy calculator,
  371. i.e. it calculates fuel consumption statistics for a motor vehicle.
  372.  
  373. The program displays its results on the terminal screen, and optionally
  374. will produce a short statement on the standard line printer.
  375.  
  376. There are three versions of the program, based on the fact that there are
  377. three (or rather, two and one half) systems of measurement commonly used
  378. to measure distance travelled and fuel consumed.
  379.  
  380. .B USMPG
  381. is the American version of the program, requesting input in miles and 
  382. US gallons.
  383.  
  384. .B UKMPG
  385. is the British version of the program, requesting input in miles and Imperial
  386. gallons. Obviously, it is useful anywhere where this combination of
  387. measurements is used.
  388.  
  389. .B EURMPG
  390. is the European or Decimal version of the program, requesting input in
  391. kilometers and liters. It is useful wherever the metric system has been fully
  392. implemented.
  393.  
  394. All three versions of the program print their results both for the current
  395. tank filling, and for the average from the first time a particular user
  396. used the program (old data for this purpose is kept in a file in the user's
  397. home directory.
  398.  
  399. All three versions print their results in three different formats:
  400. .nf
  401.  
  402. -  Miles per US Gallon
  403. -  Miles per Imperial Gallon
  404. -  Liters per 100 Kilometers
  405.  
  406. .fi
  407. which to my knowledge covers all common conventions to measure fuel efficiency.
  408.  
  409. The datafile $HOME/.mpgdata contains the total figures since the first time
  410. a particular user used the program, in the form of a single line of text.
  411. This line consists of five colon-delimited fields which contain the total miles,
  412. kilometers, US gallons, UK gallons, and liters. Here is a sample of the
  413. $HOME/.mpgdata file:
  414.  
  415. .nf
  416.     598.35:957.36:21.50:18.15:81.71
  417.        |      |     |     |     |
  418.     miles     |  US Gall. |   Liters
  419.          kilometers     UK Gall.
  420. .fi
  421. .SH INSTALLATION
  422. Make sure the floating point arithmetic commands (fpdivide.1) are available.
  423. Copy the shell scripts for the three versions to your local bin directory,
  424. and make an additional link
  425. .I mpg
  426. to the version you will most frequently use.
  427. Copy the file
  428. .I mpgdata
  429. to $HOME/.mpgdata for every user who will use the program.
  430. The file is compatible with all versions of the program, so the versions can
  431. be used alternatively with no modification to #HOME/.mpgdata necessary.
  432. .SH FILES
  433. $HOME/.mpgdata    the datafile
  434. .SH AUTHOR
  435. Wolf N. Paul (ihnp4!killer!wnp)
  436. .br
  437. Released to the Public Domain Jan 18, 1988
  438. SHAR_EOF
  439. fi # end of overwriting check
  440. if test -f 'mpgdata'
  441. then
  442.     echo shar: will not over-write existing file "'mpgdata'"
  443. else
  444. cat << \SHAR_EOF > 'mpgdata'
  445. 0.0:0.0:0.0:0.0:0.0
  446. SHAR_EOF
  447. fi # end of overwriting check
  448. if test -f 'ukmpg'
  449. then
  450.     echo shar: will not over-write existing file "'ukmpg'"
  451. else
  452. cat << \SHAR_EOF > 'ukmpg'
  453. :
  454. # ukmpg  (Miles Per Gallon -- UK Version) -- A Fuel Economy Calculator
  455. # This shell program uses the floating point arithmetic commands
  456. # "fp*" to calculate fuel economy in terms of liters per 100 km,
  457. # miles per US gallon, and miles per UK (Imperial) gallon.
  458. #
  459. # As distributed, the program assumes that the user will enter 
  460. # the distance travelled in MILES, and the amount of fuel used
  461. # in UK GALLONS, but that is not difficult to change to any other
  462. # combination of volume and distance measuring units.
  463. #
  464. # Author: Wolf N. Paul (ihnp4!killer!dcs!wnp), 1/13/88
  465. # Released into the Public Domain, Jan 18, 1988
  466. #
  467. # set -v # enable debugging
  468. #
  469. # CONSTANTS:
  470. #
  471. FILE=$HOME/.mpgdata
  472. US_GALLON=3.8
  473. UK_GALLON=4.5
  474. MILE=1.6
  475. #
  476. # Variables - all initialized to 0.0
  477. C_MILES=0.0        # current miles
  478. C_KMS=0.0        # current kilometers
  479. C_USGAL=0.0        # current gallons US
  480. C_UKGAL=0.0        # current gallons UK
  481. C_LITERS=0.0    # current liters
  482. T_MILES=0.0        # total miles
  483. T_KSM=0.0        # total kilometers
  484. T_USGAL=0.0        # total gallons US
  485. T_UKGAL=0.0        # total gallons UK
  486. T_LITERS=0.0    # total liters
  487. C_USMPG=0.0        # current miles/gallon US
  488. C_UKMPG=0.0        # current miles/gallon UK
  489. C_LP100=0.0        # current liters/100 km
  490. A_USMPG=0.0    # average miles/gallon US
  491. A_UKMPG=0.0    # average miles/gallon UK
  492. A_LP100=0.0    # average liters/100 km
  493. TMP=0.0            # kms/100
  494.  
  495. echo "MPG - Fuel Economy Calculator\n"
  496. echo "UK Version - enter Miles and Imperial Gallons\n"
  497.  
  498. echo "Reading old values ... \c"
  499.  
  500. T_MILES=`cut -d: -f1 $FILE`
  501. T_KMS=`cut -d: -f2 $FILE`
  502. T_USGAL=`cut -d: -f3 $FILE`
  503. T_UKGAL=`cut -d: -f4 $FILE`
  504. T_LITERS=`cut -d: -f5 $FILE`
  505. echo "done!\n"
  506.  
  507. echo "Enter miles driven: \c"
  508. read C_MILES
  509.  
  510. echo "Enter gallons used: \c"
  511. read C_UKGAL
  512.  
  513. echo "\nOne moment - calculating consumption figures ... \c"
  514. C_KMS=`fpmultiply $C_MILES $MILE`
  515. C_LITERS=`fpmultiply $C_UKGAL $UK_GALLON`
  516. C_USGAL=`fpdivide $C_LITERS $US_GALLON`
  517.  
  518. T_MILES=`fpadd $T_MILES $C_MILES`
  519. T_KMS=`fpadd $T_KMS $C_KMS`
  520. T_USGAL=`fpadd $T_USGAL $C_USGAL`
  521. T_UKGAL=`fpadd $T_UKGAL $C_UKGAL`
  522. T_LITERS=`fpadd $T_LITERS $C_LITERS`
  523.  
  524. C_USMPG=`fpdivide $C_MILES $C_USGAL`
  525. A_USMPG=`fpdivide $T_MILES $T_USGAL`
  526.  
  527. C_UKMPG=`fpdivide $C_MILES $C_UKGAL`
  528. A_UKMPG=`fpdivide $T_MILES $T_UKGAL`
  529.  
  530. TMP=`fpdivide $C_KMS 100.00`
  531. C_LP100=`fpdivide $C_LITERS $TMP`
  532. TMP=`fpdivide $T_KMS 100.00`
  533. A_LP100=`fpdivide $T_LITERS $TMP`
  534.  
  535. echo "$T_MILES:$T_KMS:$T_USGAL:$T_UKGAL:$T_LITERS" > $FILE
  536.  
  537. echo "done!"
  538. echo "\nFuel Economy:\n"
  539. echo "\t$C_UKMPG miles per UK gallon\t(Average ${A_UKMPG})"
  540. echo "\t$C_USMPG miles per US gallon\t(Average ${A_USMPG})"
  541. echo "\t$C_LP100 liters per 100 km\t\t(Average ${A_LP100})"
  542. echo "\nDo you want a printout of these results? \c"
  543. read REPLY
  544. if [ "$REPLY" != "y" -a "$REPLY" != "Y" ] ; then
  545.     exit
  546. fi
  547. echo "
  548. MPG (UK) -- Fuel Economy Calculator
  549. Fuel Economy Statement as of `date '+%h %d, 19%y'`
  550.  
  551. Miles this Filling:        $C_MILES\t($C_KMS km)
  552. Gallons this Filling:      $C_UKGAL\t($C_USGAL US, $C_LITERS liters)
  553.  
  554. Total Miles so far:        $T_MILES\t($T_KMS km)
  555. Total Gallons so far:      $T_UKGAL\t($T_USGAL US, $T_LITERS liters)
  556.  
  557. Current Miles per Gallon:  $C_UKMPG\t($C_USMPG US, $C_LP100 L/100 km)
  558. Average Miles per Gallon:  $A_UKMPG\t($A_USMPG US, $A_LP100 L/100 km)\
  559. " | lp -s &
  560. SHAR_EOF
  561. fi # end of overwriting check
  562. if test -f 'usmpg'
  563. then
  564.     echo shar: will not over-write existing file "'usmpg'"
  565. else
  566. cat << \SHAR_EOF > 'usmpg'
  567. :
  568. # usmpg  (Miles Per Gallon -- US Version) -- A Fuel Economy Calculator
  569. # This shell program uses the floating point arithmetic commands
  570. # "fp*" to calculate fuel economy in terms of liters per 100 km,
  571. # miles per US gallon, and miles per UK (Imperial) gallon.
  572. #
  573. # As distributed, the program assumes that the user will enter 
  574. # the distance travelled in MILES, and the amount of fuel used
  575. # in US GALLONS, but that is not difficult to change to any other
  576. # combination of volume and distance measuring units.
  577. #
  578. # Author: Wolf N. Paul (ihnp4!killer!dcs!wnp), 1/13/88
  579. # Released into the Public Domain, Jan 18, 1988
  580. #
  581. # set -v # enable debugging
  582. #
  583. # CONSTANTS:
  584. #
  585. FILE=$HOME/.mpgdata
  586. US_GALLON=3.8
  587. UK_GALLON=4.5
  588. MILE=1.6
  589. #
  590. # Variables - all initialized to 0.0
  591. C_MILES=0.0        # current miles
  592. C_KMS=0.0        # current kilometers
  593. C_USGAL=0.0        # current gallons US
  594. C_UKGAL=0.0        # current gallons UK
  595. C_LITERS=0.0    # current liters
  596. T_MILES=0.0        # total miles
  597. T_KSM=0.0        # total kilometers
  598. T_USGAL=0.0        # total gallons US
  599. T_UKGAL=0.0        # total gallons UK
  600. T_LITERS=0.0    # total liters
  601. C_USMPG=0.0        # current miles/gallon US
  602. C_UKMPG=0.0        # current miles/gallon UK
  603. C_LP100=0.0        # current liters/100 km
  604. A_USMPG=0.0    # average miles/gallon US
  605. A_UKMPG=0.0    # average miles/gallon UK
  606. A_LP100=0.0    # average liters/100 km
  607. TMP=0.0            # kms/100
  608.  
  609. echo "MPG - Fuel Economy Calculator\n"
  610. echo "US Version - Enter Miles and US Gallons\n"
  611.  
  612. echo "Reading old values ... \c"
  613.  
  614. T_MILES=`cut -d: -f1 $FILE`
  615. T_KMS=`cut -d: -f2 $FILE`
  616. T_USGAL=`cut -d: -f3 $FILE`
  617. T_UKGAL=`cut -d: -f4 $FILE`
  618. T_LITERS=`cut -d: -f5 $FILE`
  619. echo "done!\n"
  620.  
  621. echo "Enter miles driven: \c"
  622. read C_MILES
  623.  
  624. echo "Enter gallons used: \c"
  625. read C_USGAL
  626.  
  627. echo "\nOne moment - calculating consumption figures ... \c"
  628. C_KMS=`fpmultiply $C_MILES $MILE`
  629. C_LITERS=`fpmultiply $C_USGAL $US_GALLON`
  630. C_UKGAL=`fpdivide $C_LITERS $UK_GALLON`
  631.  
  632. T_MILES=`fpadd $T_MILES $C_MILES`
  633. T_KMS=`fpadd $T_KMS $C_KMS`
  634. T_USGAL=`fpadd $T_USGAL $C_USGAL`
  635. T_UKGAL=`fpadd $T_UKGAL $C_UKGAL`
  636. T_LITERS=`fpadd $T_LITERS $C_LITERS`
  637.  
  638. C_USMPG=`fpdivide $C_MILES $C_USGAL`
  639. A_USMPG=`fpdivide $T_MILES $T_USGAL`
  640.  
  641. C_UKMPG=`fpdivide $C_MILES $C_UKGAL`
  642. A_UKMPG=`fpdivide $T_MILES $T_UKGAL`
  643.  
  644. TMP=`fpdivide $C_KMS 100.00`
  645. C_LP100=`fpdivide $C_LITERS $TMP`
  646. TMP=`fpdivide $T_KMS 100.00`
  647. A_LP100=`fpdivide $T_LITERS $TMP`
  648.  
  649. echo "$T_MILES:$T_KMS:$T_USGAL:$T_UKGAL:$T_LITERS" > $FILE
  650.  
  651. echo "done!"
  652. echo "\nFuel Economy:\n"
  653. echo "\t$C_USMPG miles per US gallon\t(Average ${A_USMPG})"
  654. echo "\t$C_UKMPG miles per UK gallon\t(Average ${A_UKMPG})"
  655. echo "\t$C_LP100 liters per 100 km\t\t(Average ${A_LP100})"
  656. echo "\nDo you want a printout of these results? \c"
  657. read REPLY
  658. if [ "$REPLY" != "y" -a "$REPLY" != "Y" ] ; then
  659.     exit
  660. fi
  661. echo "
  662. MPG (US) -- Fuel Economy Calculator
  663. Fuel Economy Statement as of `date '+%h %d, 19%y'`
  664.  
  665. Miles this Filling:        $C_MILES\t($C_KMS km)
  666. Gallons this Filling:      $C_USGAL\t($C_UKGAL UK, $C_LITERS liters)
  667.  
  668. Total Miles so far:        $T_MILES\t($T_KMS km)
  669. Total Gallons so far:      $T_USGAL\t($T_UKGAL UK, $T_LITERS liters)
  670.  
  671. Current Miles per Gallon:  $C_USMPG\t($C_UKMPG UK, $C_LP100 l/100 km)
  672. Average Miles per Gallon:  $A_USMPG\t($A_UKMPG UK, $A_LP100 l/100 km)\
  673. " | lp -s &
  674. SHAR_EOF
  675. chmod +x 'usmpg'
  676. fi # end of overwriting check
  677. #    End of shell archive
  678. exit 0
  679.